//package com.dmz.proxy.config;
//
//import com.dmz.proxy.util.GMUtil;
//import org.springframework.boot.SpringApplication;
//import org.springframework.boot.env.EnvironmentPostProcessor;
//import org.springframework.core.env.ConfigurableEnvironment;
//import org.springframework.core.env.MapPropertySource;
//import org.springframework.core.env.PropertySource;
//
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @author Laptop
// */
//public class SSLPasswordDecryptor implements EnvironmentPostProcessor {
//
//    @Override
//    public void postProcessEnvironment(ConfigurableEnvironment environment,
//                                       SpringApplication application) {
//        environment.getPropertySources()
//                  .forEach(propertySource -> decryptSSLPassword(propertySource, environment));
//    }
//
//    private void decryptSSLPassword(PropertySource<?> propertySource,
//                                    ConfigurableEnvironment environment) {
//        if (propertySource.containsProperty("server.ssl.key-store-password")
//            && propertySource.containsProperty("sm4.key") && propertySource.containsProperty("iv")) {
//            Object propertyValue = propertySource.getProperty("server.ssl.key-store-password");
//            Object sm4Key = propertySource.getProperty("sm4.key");
//            Object sm4Iv = propertySource.getProperty("sm4.iv");
//            if (propertyValue instanceof String && sm4Key instanceof String && sm4Iv instanceof String) {
//                String value = (String) propertyValue;
//                String skey = (String) sm4Key;
//                String siv = (String) sm4Iv;
//                if (value.startsWith("enc:")) {
//                    String encrypted = value.substring("enc:".length());
//                    // 自定义解密方法
//                    String decrypted = GMUtil.db(skey, siv, encrypted);
//
//                    // 将解密后的值设置到环境变量中
//                    Map<String, Object> decryptedProperties = new HashMap<>();
//                    decryptedProperties.put("server.ssl.key-store-password", decrypted);
//                    environment.getPropertySources()
//                              .addFirst(new MapPropertySource("application.yml", decryptedProperties));
//                }
//            }
//        }
//    }
//}